package cn.stylefeng.guns.sys.modular.system.utils;

import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.RequestEmptyException;

import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SysUtil {

    public static final  String PHONE_REGEX = "^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(17[013678])|(18[0,5-9]))\\d{8}$";

    /**
     * 验证手机号码格式
     * @param phone
     * @return
     */
    public static Boolean vailPhone(String phone){
        if (ToolUtil.isOneEmpty(phone)){
            return false;
        }
        if(phone.length() != 11){
            throw new RequestEmptyException("手机号码必须为11位");
        }else{
            Pattern p = Pattern.compile(PHONE_REGEX);
            Matcher m = p.matcher(phone);
            boolean isMatch = m.matches();
            if(isMatch){
                return true;
            } else {
                return false;
            }
        }
    }

    /**
     *生成6位随机字符的盐
     * @return
     */
    public static String getSalt(){

        char[] c= charArray();//获取包含26个字母大小写和数字的字符数组

        Random rd = new Random();
        String code="";
        for (int k = 0; k < 6; k++) {
            int index = rd.nextInt(c.length);//随机获取数组长度作为索引
            code+=c[index];//循环添加到字符串后面
        }
        return code;
    }

    /**
     * 包含了二十六个字母和十个数字的字符数组
     */
    public static char[] charArray(){
        int i = 1234567890;
        String s ="qwertyuiopasdfghjklzxcvbnm";
        String S=s.toUpperCase();
        String word=s+S+i;
        char[] c=word.toCharArray();
        return c;
    }

    public static void main(String[] args) {
        System.out.println(getSalt());
    }

}
